package com.leo.biz.service.datakeep;

import com.leo.biz.po.DailyKRecord;
import com.leo.biz.po.StockBasicInfo;
import com.leo.biz.po.TDkFetchTask;
import com.leo.biz.service.fetcher.TxStockFetcher;
import com.leo.biz.service.pub.CommUtil;
import com.leo.biz.service.pub.DaoCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created by leo on 2016/6/28.
 */
@Service
public class FetchTaskManager {

    @Autowired
    private DaoCenter daoCenter;

    @Autowired
    private TxStockFetcher baseStockFetcher;

    private int createfetchdone =0;
    private int fetchdone=0;

    public void run()
    {
        //System.out.println("[" + CommUtil.getCurDate() + "]:FetchTaskManager run ");
        long createleftSecs= CommUtil.difToCurDate("2016-07-30 17:38:00");
        long fetchleftSecs= CommUtil.difToCurDate("2016-07-30 17:39:00");

        if(fetchdone==1)
        {
            System.out.println("["+ CommUtil.getCurDate()+"]:FetchTaskManager.fetchDataByTaskList be called");
            int ret=fetchDataByTaskList();
            fetchdone=1;
        }

    }
    /*1 为每个stock建立今天的行情获取任务
    * 2 获取任务接口
    * 3 完成任务接口
    * */
    public int  createFetchTaskToday()
    {
        int delret=daoCenter.delteResult(TDkFetchTask.class, "");

        List<StockBasicInfo> ret =daoCenter.queryResult(StockBasicInfo.class, "");
        for(int i=0;i<ret.size();i++)
        {
            TDkFetchTask _temp=new TDkFetchTask();
            _temp.setIsdone(0);
            _temp.setStockno(ret.get(i).getStockno());
            _temp.setCldate(CommUtil.getTodayCldate());
            daoCenter.insertResult(_temp);
        }
        return 0;
    }

    public int fetchDataByTaskList()
    {
        List<String> faildMap=new LinkedList<>();
        int isAllFetched=1;
        int curDate=baseStockFetcher.getCurTradeDay();
        List<StockBasicInfo> _temp=daoCenter.queryResult(StockBasicInfo.class, "");
        List<DailyKRecord> fetchedList=daoCenter.queryResult(DailyKRecord.class, "where cldate=" + curDate);
        Map<String,DailyKRecord> fetchedMap= CommUtil.convertToHashMap(fetchedList, "stockno");
        int nAllNeedFetched=_temp.size();
        int nThisTimeFetched=0;
        for(int i=0;i<_temp.size();i++)
        {
            if(fetchedMap.containsKey(_temp.get(i).getStockno())==false)
            {
                int ret=baseStockFetcher.getToadyStockInfo(_temp.get(i).getStockno());
                if(ret==0)
                {
                    isAllFetched=0;
                    CommUtil.log("DailyKRecord Get Failed! Seq= " + i + " stockno=" + _temp.get(i).getStockno());
                    faildMap.add(_temp.get(i).getStockno());
                }else
                {
                    CommUtil.log("DailyKRecord Getted! Seq= " + i + " stockno=" + _temp.get(i).getStockno());
                    nThisTimeFetched++;
                }
            }

        }
        CommUtil.log("nAllNeedFetched=" + nAllNeedFetched + ",nThisTimeFetched=" + nThisTimeFetched + " nThisTimeFailed=" + faildMap.size());
        String failedStr="========Failed Fetched StockNo==========\n    ";
        for(int i=0;i<faildMap.size();i++)
        {
            if(i%5==0)
            {
                failedStr=failedStr+" "+faildMap.get(i)+"\n    ";
            }else
            {
                failedStr=failedStr+" "+faildMap.get(i);
            }

        }
        CommUtil.log(failedStr);
        return isAllFetched;
    }



}
